<template>
  <div>
   
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item>
    </el-breadcrumb> -->
     <!--封装的面包屑---------------------------------------------------------------->
    <my-bread :breadList='breadList'></my-bread>

    <el-row>
      <el-col :span="13">
        <el-input
          placeholder="请输入搜索内容"
          v-model="search"
          class="input-with-select"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="searchFn"
          ></el-button>
        </el-input>
      </el-col>
      <el-col :span="3" :offset="1">
        <el-button type="primary" @click="dialogFormVisible = true"
          >添加用户</el-button
        >
      </el-col>
    </el-row>
    <!--table表格---------------------------------------------------------------->
    <template>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" prop="date" label="#"> </el-table-column>
        <el-table-column prop="username" label="姓名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column prop="mg_state" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="edit(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <!--操作---------------------------------------------------------------->
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <!--修改数据---------------------------------------------------------------->
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="editor(scope.row)"
            ></el-button>
            <!--分配角色---------------------------------------------------------------->
            <el-button
              type="warning"
              icon="el-icon-setting"
              circle
              @click="Assigningdialogs(scope.row)"
            ></el-button>
            <!--删除---------------------------------------------------------------->
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="delFn(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!--添加用户弹出框---------------------------------------------------------------->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="addRules" ref="addform">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input v-model="form.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add('addform')">确 定</el-button>
      </div>
    </el-dialog>
    <!--编辑用户弹出框---------------------------------------------------------------->
    <el-dialog title="添加用户" :visible.sync="dialog">
      <el-form :model="Editform">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input
            v-model="Editform.username"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="Editform.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input v-model="Editform.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary" @click="editors">确 定</el-button>
      </div>
    </el-dialog>
    <!--分配角色弹出框---------------------------------------------------------------->

    <el-dialog title="收货地址" :visible.sync="Assigningdialog">
      <el-form :model="Assigningform">
        <el-form-item label="当前的用户" :label-width="formLabelWidth">
          {{ Assigningform.username }}
        </el-form-item>
        <el-form-item label="当前的角色" :label-width="formLabelWidth">
          {{ Assigningform.role_name }}
        </el-form-item>
        <el-form-item label="分配新角色" :label-width="formLabelWidth">
          <el-select
            v-model="Assigningform.deal"
            @change="changeName"
            placeholder="请选择"
          >
            <el-option
              v-for="item in rolseList"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="Assigningdialog = false">取 消</el-button>
        <el-button type="primary" @click="DealRoles">确 定</el-button>
      </div>
    </el-dialog>
    <!--分页---------------------------------------------------------------->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.pagenum"
      :page-sizes="[2, 5, 10, 20]"
      :page-size="page.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
import {
  users,
  Adduser,
  Delusers,
  isEdit,
  UserID,
  Editor,
  dealUser,
  UserRoles
} from "@/http/user.js";
import _ from "lodash";
import bread from '../components/myui/bread.vue'
export default {
  components:{
    bread
  },
  data() {
    return {
      breadList:[
        {"path":'/home',"title":'首页'},
        {"path":'/users',"title":'用户管理'},
        {"path":'/users',"title":'用户列表'},
      ],
      query: "",
      search: "",
      value: true,
      tableData: [],
      rolseList:[],
      dialogFormVisible: false,
      dialog: false,
      form: {
        username: "",
        password: "",
        mobile: "",
        email: "",
      },

      Editform: {
        username: "",
        mobile: "",
        email: "",
        EditID: 0,
      },
      Assigningform: {
        username: "",
        role_name: "",
        deal: "",
      },
      id: 0,
      rid: 0,
      Assigningdialog: false,
      page: {
        pagenum: 1,
        pagesize: 5,
      },
      formLabelWidth: "100px",
      total: 0,

      addRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 封装的渲染数据列表----------------------------------------------------------
    async userFn() {
      let res = await users({
        query: this.query,
        // 当前哪一页
        pagenum: this.page.pagenum,
        // 每一页几条
        pagesize: this.page.pagesize,
      });
      this.tableData = res.data.users;
      this.total = res.data.total;
      console.log(res);
    },
    // 添加数据----------------------------------------------------------
    add(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await Adduser(this.form);
          // console.log(res);
          if (res.meta.status == 201) {
            this.dialogFormVisible = false;
            this.userFn();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 搜索数据列表----------------------------------------------------------
    // searchFn() {
    //   this.query = this.search;
    //   this.userFn();
    // },
    searchFn: _.debounce(function () {
      console.log(this); // undefined
      this.query = this.search;
      (this.page.pagenum = 1), this.userFn();
    }, 2000),
    // 删除数据列表----------------------------------------------------------
    async delFn(id) {
      // console.log(id);
      let res = await Delusers(id);
      // console.log(res);
      this.userFn();
    },
    // 切换状态----------------------------------------------------------
    async edit(val) {
      // console.log(val);
      let res = await isEdit(val.id, val.mg_state);
      // console.log(res);
      this.userFn();
    },

    // 编辑数据列表----------------------------------------------------------
    async editor(val) {
      // console.log(val);
      let res = await UserID(val.id);
      console.log(res);
      this.Editform.EditID = res.data.id;
      this.Editform.username = res.data.username;
      this.Editform.mobile = res.data.mobile;
      this.Editform.email = res.data.email;
      this.dialog = true;
    },
    async editors() {
      let res = await Editor(this.Editform);
      // console.log(this.Editform);
      // console.log(res);
      this.dialog = false;
      this.userFn();
    },
    //分配角色--------------------------------------------------------
    Assigningdialogs(val) {
      // console.log(res);
      this.Assigningdialog = true;
      this.Assigningform = val;
      this.id=val.id
      //角色列表的id
      UserRoles().then(res=>{
        console.log(res);
        this.rolseList=res.data
      })
    },
    // id 本行信息的id rid 角色列表的id
    DealRoles() {
      dealUser({
        id: this.id,
        rid: this.rid,
      }).then((res) => {
        console.log(res);
        this.userFn();
      });
      // console.log(this.id,this.rid);
      this.Assigningdialog = false;
    },
    // 分页----------------------------------------------------------------
    // 一页多少条----------------------------------------------------------
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.page.pagesize = val;
      this.userFn();
    },
    // 点击的哪一页----------------------------------------------------------
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page.pagenum = val;
      this.userFn();
    },
    changeName(rid) {
      console.log(rid);
      this.rid = rid;
    },
  },
  created() {
    this.userFn();
  },
};
</script>
<style lang="scss" scoped>
.el-row {
  // 上右下左
  margin: 10px 0;
}

.input-with-select .el-input-group__prepend {
  background-color: #fff;
}
</style>